% function estimaLoopSingle
%% 7. Begin Estimation Loop
parfor ii=1:nstrvals;
    
    %% 7a) Parvecmode is the mode
    parvecmode            =zeros(numpar, 1);
    parvecmode(parposcal) =parcalval;
    
    %% 7b) Optimization Routine, output is optimOut
    disp(['Starting Value',num2str(ii)]);
    
    optimOut=feval(funcOptimization,parstrvals(parposest,ii),estimopt,Y,...
        funcmod,solveopt,addsol,prpar,prss,estimStructure);
    
    %% 7c) Assing fields of optimOut
    parvecmode(parposest)= optimOut.mode;
    parestmat(:,ii)=optimOut.mode;
    parmodemat(:,ii)=parvecmode;
    stdLoopMat(:,ii)=sqrt(diag(optimOut.Hessian));
    hessianMat(:,:,ii)=optimOut.Hessian;
    
    %% 7d) Compute Posterior (old syntaxis, replace by passing structure)
    [logpostd_mat(ii,2),loglikel_mat(ii,2)]=...
        feval(@lmjPosterior,parestmat(:,ii),parvecmode,parposest,funcmod,Y,trainvec,prpar,prss,solveopt,addsol,ssposest);
    logprior_mat(ii,2)=logpostd_mat(ii,2)-loglikel_mat(ii,2);
    
    %     if ~isnan(optimOut.Hessian)
    %         stdLoopMat(:,ii)=sqrt(diag(optimOut.Hessian));
    %     end
    
    try
        disp(['Result from estimation ',feval(@num2str,ii)]);
        feval(@printcell,[parnames(parposest) feval(@num2cprec,optimOut.mode)]);
        dispaj('Log Posterior = ',logpostd_mat(ii,2));
        dispaj('Log Likelihood= ',loglikel_mat(ii,2));
    catch
        lasterr
    end

    mat_exitflag(ii)=optimOut.exitFlag;
    mat_iterations(ii)=optimOut.iterations;
    mat_time(ii)=optimOut.time/60;
    
end